package van.planifolia.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import van.planifolia.pojo.Role;
import van.planifolia.service.RoleService;
import van.planifolia.vo.ComResult;
import van.planifolia.vo.RoleVo;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA<br>
 * 角色的Controller层
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/10/11 14:51
 */
@RestController
@RequestMapping("/Role")
public class RoleController {
    /**
     * 引入Service
     */
    @Resource(name = "roleService")
    RoleService roleService;

    /**
     * 查询当前用户的角色信息
     * @param uid 用户id
     * @return 当前用户的角色信息以及全部角色信息
     */
    @PostMapping("/UserRolesMsg")
    public ComResult userRolesMsg(Integer uid) {
        Map<String, Object> allRoleAndUserRole = roleService.getAllRoleAndUserRole(uid);
        return new ComResult(2000, "查询成功", allRoleAndUserRole);
    }

    /**
     * 修改用户的角色信息
     * @param uid 用户id
     * @param roles 角色们
     * @return 通用的结果集
     */
    @PostMapping("/ModifyUserRole")
    public ComResult removeUserRole(Integer uid,Integer[] roles){
        boolean b = roleService.modifyRoleByUid(uid,roles);
        if (b){
            return new ComResult(2000,"修改成功！");
        }else {
            return new ComResult(5001,"修改失败！");
        }
    }

    /**
     * 获取全部角色信息
     * @param roleVo 查询角色信息的条件
     * @return 全部的角色信息
     */
    @PostMapping("/GetAllRole")
    public ComResult getAllRole(RoleVo roleVo){
        List<Role> allRole = roleService.getAllRole(roleVo);
        return  new ComResult(2000,"查询成功",allRole);
    }

}
